// DAO/Impl/AssignmentSubmissionDaoImpl.java
package com.hp.DAO.Impl;                         // 实现包

import com.hp.DAO.AssignmentSubmissionDao;       // 接口：AssignmentSubmissionDao
import com.hp.DAO.BaseDao;                       // 基类
import com.hp.Model.AssignmentSubmission;        // 实体：作业提交
import com.hp.Util.RowMapper;                    // 行映射器
import com.hp.Util.SimpleJdbcTemplate;           // JDBC 模板

import java.sql.Timestamp;                // 时间戳
import java.util.Date;                    // Date
import java.util.List;                    // List

/** AssignmentSubmissionDao 的 JDBC 实现 */
public class AssignmentSubmissionDaoImpl extends BaseDao implements AssignmentSubmissionDao {
    public AssignmentSubmissionDaoImpl(SimpleJdbcTemplate jdbc) { super(jdbc); } // 构造注入

    // RowMapper：ResultSet -> AssignmentSubmission
    private static final RowMapper<AssignmentSubmission> M = rs -> {
        AssignmentSubmission s = new AssignmentSubmission();               // 新建实体
        s.setId(rs.getLong("id"));                                         // id
        s.setAssignmentId(rs.getLong("assignment_id"));                    // 作业ID
        s.setStudentId(rs.getLong("student_id"));                          // 学生ID
        s.setContentText(rs.getString("content_text"));                    // 文本内容
        s.setFileUrl(rs.getString("file_url"));                            // 附件URL
        Timestamp sub = rs.getTimestamp("submitted_at");                   // 提交时间
        Timestamp upd = rs.getTimestamp("updated_at");                     // 更新时间
        s.setSubmittedAt(sub == null ? null : new Date(sub.getTime()));    // 转 Date
        s.setUpdatedAt(upd == null ? null : new Date(upd.getTime()));      // 转 Date
        s.setStatus(rs.getString("status"));                               // 状态
        return s;                                                          // 返回
    };

    @Override
    public AssignmentSubmission findById(Long id) {                        // 按主键查询
        String sql = "SELECT id,assignment_id,student_id,content_text,file_url,submitted_at,updated_at,status " +
                "FROM assignment_submission WHERE id=?";              // SQL
        return queryOne(sql, M, id);                                       // 执行
    }

    @Override
    public AssignmentSubmission findByAssignmentAndStudent(Long assignmentId, Long studentId) { // 按作业+学生唯一
        String sql = "SELECT id,assignment_id,student_id,content_text,file_url,submitted_at,updated_at,status " +
                "FROM assignment_submission WHERE assignment_id=? AND student_id=?";        // SQL
        return queryOne(sql, M, assignmentId, studentId);                                       // 执行
    }

    @Override
    public List<AssignmentSubmission> listByAssignment(Long assignmentId, int offset, int limit) { // 按作业分页
        String sql = "SELECT id,assignment_id,student_id,content_text,file_url,submitted_at,updated_at,status " +
                "FROM assignment_submission WHERE assignment_id=? ORDER BY id DESC LIMIT ? OFFSET ?";      // SQL
        return queryList(sql, M, assignmentId, limit, offset);                                  // 执行
    }

    @Override
    public Long insert(AssignmentSubmission s) {                           // 新增提交
        String sql = "INSERT INTO assignment_submission (assignment_id,student_id,content_text,file_url,submitted_at,updated_at,status) " +
                "VALUES (?,?,?,?,?,?,?)";                             // SQL
        Date now = new Date();                                             // 当前时间
        Timestamp submitted = s.getSubmittedAt() == null ? new Timestamp(now.getTime())
                : new Timestamp(s.getSubmittedAt().getTime());         // 提交时间
        Timestamp updated = s.getUpdatedAt() == null ? new Timestamp(now.getTime())
                : new Timestamp(s.getUpdatedAt().getTime());               // 更新时间
        return execInsertReturnId(sql, s.getAssignmentId(), s.getStudentId(), s.getContentText(), s.getFileUrl(),
                submitted, updated, s.getStatus());                        // 执行插入
    }

    @Override
    public int updateContentAndStatus(AssignmentSubmission s) {            // 更新内容与状态
        String sql = "UPDATE assignment_submission SET content_text=?, file_url=?, status=?, updated_at=? WHERE id=?"; // SQL
        Timestamp updated = s.getUpdatedAt() == null ? new Timestamp(System.currentTimeMillis())
                : new Timestamp(s.getUpdatedAt().getTime());                 // 更新时间
        return execUpdate(sql, s.getContentText(), s.getFileUrl(), s.getStatus(), updated, s.getId());           // 执行更新
    }

    @Override
    public int deleteById(Long id) {                                       // 删除
        String sql = "DELETE FROM assignment_submission WHERE id=?";       // SQL
        return execUpdate(sql, id);                                        // 执行删除
    }
}
